<?php


namespace app\api\controller\v1;


use app\common\controller\Api;
use think\Db;

/**
 * 会员卡接口
 */
class Good extends Api
{

    protected $noNeedLogin = ['index'];
    protected $noNeedRight = '*';
    protected static $type = [
        1 => '次卡',
        2 => '私教卡',
        3 => '周卡',
        4 => '月卡',
        5 => '学生卡'
    ];
    protected static $discount = [
        0 => '不是优惠卡',
        1 => '优惠券指定卡'
    ];

    protected function _initialize()
    {
        parent::_initialize(); // TODO: Change the autogenerated stub
    }

    /**
     * 会员卡列表、详情：分别传入参数返回各自的列表或者详情，参数的优先级：会员卡主键<教练主键<场馆主键
     * @ApiMethod (GET)
     * @param int $id 会员卡主键：id
     * @param int $coach_id 教练主键：coach_id
     * @param int $gymnasium_id 场馆主键：gymnasium_id
     */
    public function index()
    {
        if ($this->request->isGet()) {

            $gymnasium_id = input('gymnasium_id');
            $coach_id     = input('coach_id');
            $id           = input('id');

            if (!$gymnasium_id && !$coach_id && !$id) {
                $this->error('必传一个参数');
            }

            if ($id) {
                $result = Db::name('good')
                    ->alias('good')
                    ->join('gymnasium gymnasium', 'gymnasium.id = good.gymnasium_id', 'left')
                    ->join('coach coach', 'coach.id = good.coach_id', 'left')
                    ->field('good.is_discount,good.stock,good.gymnasium_id,end_number,good.coach_id,good.type,good.name,good.title,good.price,good.full_image,good.content_,good.status,gymnasium.name as gymnasium_name,coach.name as coach_name')
                    ->find(['good.id' => $id]);

                if ($result) {
                    $result['status'] != 1 && $this->error('获取失败,该会员卡已下架');
                    unset($result['status']);
                    $result['full_image']       = self::patch_cdn($result['full_image']);
                    $result['type_text']        = self::$type[$result['type']];

                    $this->success('获取成功', $result);
                } else {
                    $this->error('获取失败,会员卡信息不存在');
                }
            } elseif ($coach_id) {
                $results = Db::name('good')
                    ->alias('good')
                    ->join('gymnasium gymnasium', 'gymnasium.id = good.gymnasium_id', 'left')
                    ->join('coach coach', 'coach.id = good.coach_id', 'left')
                    ->where(['good.coach_id' => $coach_id, 'good.status' => 1])
                    ->order('good.weigh', 'desc')
                    ->field('good.is_discount,good.stock,good.id,good.gymnasium_id,end_number,good.type,good.name,good.title,good.price,good.thumb_image,good.status,gymnasium.name as gymnasium_name,coach.name as coach_name')
                    ->select();
                foreach ($results as &$item) {
                    $item['thumb_image']        = self::patch_cdn($item['thumb_image']);
                    $item['type_text']          = self::$type[$item['type']];
                }
                $result['data'] = $results;
            } else {
                $results = Db::name('good')
                    ->alias('good')
                    ->join('gymnasium gymnasium', 'gymnasium.id = good.gymnasium_id', 'left')
//                    ->join('coach coach', 'coach.id = good.coach_id', 'left')
                    ->where(['good.gymnasium_id' => $gymnasium_id, 'good.status' => 1, 'good.coach_id' => 0])
                    ->order('good.weigh', 'desc')
//                    ->field('good.coach_id,good.type,good.name,good.title,good.price,good.thumb_image,good.status,gymnasium.name as gymnasium_name,coach.name as coach_name')
                    ->field('good.id,end_number,good.type,good.name,good.title,good.price,good.thumb_image,good.status,gymnasium.name as gymnasium_name')
                    ->select();

                foreach ($results as &$item) {
                    $item['thumb_image'] = self::patch_cdn($item['thumb_image']);
                    $item['type_text']   = self::$type[$item['type']];
                }
                $result['data'] = $results;
            }

            $this->success('获取成功', $result);
        } else {
            $this->error(ErrorCode::$ERROR['ERROR_METHOD']);
        }
    }
}